package com.hznu.jspFinal.controller;

import com.hznu.jspFinal.entity.UserType;
import com.hznu.jspFinal.myBeans.R;
import com.hznu.jspFinal.service.UserTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author  jamesZhan
 */
@Api(description = "用户类型接口" , value = "usertype")
@RestController
@RequestMapping("/usertype")
public class UserTypeController {
    @Autowired
    private UserTypeService userTypeService;

    @ApiOperation("增加用户角色")
    @PostMapping("/addOne")
    public R addOne(@RequestBody UserType userType){
        try{
            userTypeService.insert(userType);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation("删除角色")
    @GetMapping("/remove/{id}")
    public  R remove(@PathVariable Integer id){
        try{
            userTypeService.delete(id);
            return R.ok();
        }catch(Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation("更新角色")
    @PostMapping("/update")
    public R update(@RequestBody UserType userType){
        try{
            userTypeService.update(userType);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation("查找角色")
    @GetMapping("/findOne/{id}")
    public R findOne(@PathVariable Integer id){
        try{
            return R.ok().put("data",userTypeService.select(id));
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation("获取所有的角色")
    @PostMapping("/findAll")
    public R findAll(){
        try{
            return R.ok().put("data",userTypeService.selectAll());
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

}
